package com.hushao.aimi.controller;

import com.hushao.aimi.ValidUtil;
import com.hushao.aimi.vo.AddressVo;
import com.hushao.aimi.common.ServerResponse;
import com.hushao.aimi.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @description: 地址模块
 * @author: 胡少
 * @create: 2018-06-18 12:09
 **/
@Api(value ="地址模块" )
@RestController
@Slf4j
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;


    @ApiOperation(value = "添加用户地址")
    @PostMapping("/{username}")
    @PreAuthorize("authentication.name.equals(#username)")
    public ServerResponse add(@PathVariable String username, @Valid @RequestBody AddressVo addressVo,BindingResult bindingResult)throws Exception{
        ValidUtil.valid(bindingResult);
        return addressService.add(username,addressVo);
    }


    @ApiOperation(value = "删除用户地址")
    @DeleteMapping("/{addressId}")
    public ServerResponse del(@PathVariable String addressId)throws Exception{
        return addressService.add(addressId);
    }


    @ApiOperation(value = "修改用户地址")
    @PutMapping("/{username}")
    @PreAuthorize("authentication.name.equals(#username)")
    public ServerResponse update(@PathVariable String username,@RequestBody AddressVo addressVo)throws Exception{
        return addressService.update(username,addressVo);
    }


    @ApiOperation(value = "查询用户地址详细")
    @GetMapping("/{addressId}")
    public ServerResponse detail(@PathVariable String addressId){
        return addressService.detail(addressId);
    }


    @ApiOperation(value = "查询用户地址列表")
    @GetMapping
    @PreAuthorize("authentication.name.equals(#username)")
    public ServerResponse listByUsername(@RequestParam String username){
        return addressService.listByUsername(username);
    }

}
